//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

//日记设置
struct AiSetDiary: View {
    var forceShow = false
    @EnvironmentObject var themeManager: ThemeManager
    @EnvironmentObject var appStorageManager: AppStorageManager

    @State var isShow = false
    @State private var showImageCount = false
    @State private var foldModeStr = ""
    @State private var foldStyleStr = ""

    var body: some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text("日记设置")
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                    .onTapGesture {
                        withAnimation(Animation.easeInOut(duration: 0.3)) {
                            if !forceShow {
                                isShow.toggle()
                            }
                        }
                    }
                
                if isShow{
                   
                    Toggle("AI评论", isOn: appStorageManager.$toggleAiComment)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                    Toggle("AI日记", isOn: appStorageManager.$toggleAiDiary)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                        .onChange(of: appStorageManager.toggleAiDiary) { newValue in
                            TaskAiDiaryExt.instance.updateTask()
                        }
                    Toggle("随机多人回复", isOn: appStorageManager.$isRandomMultipleToggle)
                        .foregroundColor(.bgItemContent)
                        .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                        .font(.system(size: 14))
                    Toggle(isOn: appStorageManager.$toggleAiImage, label: {
                        VStack(alignment:.leading){
                            HStack(spacing: 5) {
                                Text("图片识别")
                                    .foregroundColor(.bgItemContent)
                                    .font(.system(size: 14))
                                if appStorageManager.toggleAiImage {
                                    Text("识别数量：\(AppStorageManager.shared.aiImageCount)")
                                        .foregroundColor(.bgNormalText4)
                                        .font(.system(size: 10))
                                }
                            }
                            Text("需要读取图片内容，所以整体的回应速度减慢，\n回应次数2次/张")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }.onChange(of: appStorageManager.toggleAiImage) { newValue in
                            if newValue {
                                if userManager.isVip() {
                                    showImageCount = true
                                } else {
                                    appStorageManager.toggleAiImage = false
                                    showTipsDialog(text: "该功能是Pro专享")
                                }
                            }
                        }.confirmationDialog("", isPresented: $showImageCount){
                            Button("识别第一张") {
                                AppStorageManager.shared.aiImageCount = 1
                            }
                            Button("识别前2张") {
                                AppStorageManager.shared.aiImageCount = 2
                            }
                            Button("识别前3张") {
                                AppStorageManager.shared.aiImageCount = 3
                            }
                            Button("取消", role: .cancel) {}
                        } message: {
                            Text("请选择识别数量")
                        }
                        
                    }).toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                    //评论显示设置
                    Menu {
                        Button("仅显示当天（其余折叠）", role: AppStorageManager.shared.foldMode == IFoldMode.TYPE_DAY ? .destructive : .cancel) {
                            AppStorageManager.shared.foldMode = IFoldMode.TYPE_DAY
                        }
                        Button("仅显示当月（其余折叠）", role: AppStorageManager.shared.foldMode == IFoldMode.TYPE_MONTH ? .destructive : .cancel) {
                            AppStorageManager.shared.foldMode = IFoldMode.TYPE_MONTH
                        }
                        Button("仅显示今年（其余折叠）", role: AppStorageManager.shared.foldMode == IFoldMode.TYPE_YEAR ? .destructive : .cancel) {
                            AppStorageManager.shared.foldMode = IFoldMode.TYPE_YEAR
                        }
                        Button("全部显示（所有评论不折叠）", role: AppStorageManager.shared.foldMode == IFoldMode.TYPE_ALL ? .destructive : .cancel) {
                            AppStorageManager.shared.foldMode = IFoldMode.TYPE_ALL
                        }
                        Button("全部折叠", role: AppStorageManager.shared.foldMode == IFoldMode.TYPE_CLOSE ? .destructive : .cancel) {
                            AppStorageManager.shared.foldMode = IFoldMode.TYPE_CLOSE
                        }
                    } label: {
                        HStack {
                            Text("评论显示设置")
                                .foregroundColor(.bgItemContent)
                                .font(.system(size: 14))
                            Spacer()
                            Text(foldModeStr)
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                            Image(.appIconArrowRight)
                                .resizable()
                                .frame(width: 15,height: 15)
                            
                        }.padding(.top,5).padding(.bottom,5)
                    }
                    //折叠样式
                    Menu {
                        Button("头像", role: AppStorageManager.shared.foldStyle == IFoldStyle.TYPE_AVATAR ? .destructive : .cancel) {
                            AppStorageManager.shared.foldStyle = IFoldStyle.TYPE_AVATAR
                        }
                        Button("名称", role: AppStorageManager.shared.foldStyle == IFoldStyle.TYPE_TEXT ? .destructive : .cancel) {
                            AppStorageManager.shared.foldStyle = IFoldStyle.TYPE_TEXT
                        }
                        Button("不显示", role: AppStorageManager.shared.foldStyle == IFoldStyle.TYPE_HIDE ? .destructive : .cancel) {
                            AppStorageManager.shared.foldStyle = IFoldStyle.TYPE_HIDE
                        }
                    } label: {
                        HStack {
                            Text("折叠样式")
                                .foregroundColor(.bgItemContent)
                                .font(.system(size: 14))
                            Spacer()
                            Text(foldStyleStr)
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                            Image(.appIconArrowRight)
                                .resizable()
                                .frame(width: 15,height: 15)
                            
                        }.padding(.top,5).padding(.bottom,5)
                    }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18).background(.bgItem).clipShape(RoundedRectangle(cornerRadius: 15))
            .onChange(of: appStorageManager.foldMode) { value in
                getFoldModeStr()
            }
            .onChange(of: appStorageManager.foldStyle) { value in
                getFoldStyleStr()
            }
            .onAppear{
                if forceShow {
                    isShow = true
                }
                getFoldModeStr()
                getFoldStyleStr()
            }
    }
    
    func getFoldModeStr() {
        switch AppStorageManager.shared.foldMode {
        case .TYPE_DAY:
            foldModeStr = "仅显示当天（其余折叠）"
        case .TYPE_MONTH:
            foldModeStr = "仅显示当月（其余折叠）"
        case .TYPE_YEAR:
            foldModeStr = "仅显示今年（其余折叠）"
        case .TYPE_ALL:
            foldModeStr = "全部显示（所有评论不折叠）"
        case .TYPE_CLOSE:
            foldModeStr = "全部折叠"
        }
    }
    
    func getFoldStyleStr() {
        switch AppStorageManager.shared.foldStyle {
        case .TYPE_AVATAR:
            foldStyleStr = "头像"
        case .TYPE_TEXT:
            foldStyleStr = "名称"
        case .TYPE_HIDE:
            foldStyleStr = "不显示"
        }
    }
}

//#Preview {
//    SetPasswordView()
//}

//定义预览提供者
struct AiSetDiary_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiSetDiary(isShow: true))
    }
}
